package cn.ibobo.lease.auth.service.impl;

import cn.ibobo.lease.auth.dao.UserLoginLogMapper;
import cn.ibobo.lease.auth.dao.UserMapper;
import cn.ibobo.lease.auth.service.UserService;
import cn.ibobo.lease.common.base.BaseMapper;
import cn.ibobo.lease.common.base.BaseServiceImpl;
import cn.ibobo.lease.common.dto.AdminDTO;
import cn.ibobo.lease.common.dto.AdminLoginLogDTO;
import cn.ibobo.lease.common.dto.UserDTO;
import cn.ibobo.lease.common.dto.UserLoginLogDTO;
import cn.ibobo.lease.common.util.DateUtil;
import cn.ibobo.lease.common.util.IpUtil;
import cn.ibobo.lease.common.util.Md5Util;
import cn.ibobo.lease.common.util.UuidUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 会员信息表业务处理接口
 * @Author lililiang
 * @Date 2018-12-13 18:36:40
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserDTO, String> implements UserService {

    private static final Log LOGGER = LogFactory.getLog(UserServiceImpl.class);

    @Autowired
    private UserMapper mapper;
    @Autowired
    private UserLoginLogMapper logMapper;

    @Override
    protected BaseMapper<UserDTO, String> getMapper() {
        return mapper;
    }


    /**
     * @Description 管理员登陆
     * @Author lililiang
     * @Date 2018/12/13 19:12
     * @Param [entityDTO]
     * @Return cn.ibobo.lease.common.dto.AdminDTO
     */
    @Override
    @Transactional
    public UserDTO appLogin(UserDTO entityDTO, HttpServletRequest req) {
        //1. 获取登陆信息
        UserDTO userDTO = mapper.queryForLogin(entityDTO);
        if(userDTO == null){
            return null;
        }else{
            //2. 登陆成功后保存登陆日志
            UserLoginLogDTO logDTO = new UserLoginLogDTO();
            logDTO.setUid(userDTO.getUid());
            logDTO.setLoginip(IpUtil.getIpAddr(req));
            logDTO.setLogintm(DateUtil.getNow());
            logDTO.setId(UuidUtil.getUuid());
            logMapper.add(logDTO);
            return userDTO;
        }


    }

    /**
     * 会员信息表列表
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public List<UserDTO> list4Page(UserDTO entity, Integer pageNum, Integer... pageSize) {
        return super.list4Page(entity, pageNum, pageSize);
    }

    /**
     * 会员信息表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public List<UserDTO> listByCondition(UserDTO entity) {
        return super.listByCondition(entity);
    }

    /**
     * 查询会员信息表详情信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public UserDTO queryOneByPk(String id) {
        return super.queryOneByPk(id);
    }

    /**
     * 保存会员信息表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer save(UserDTO entity) {
        //1.对密码进行加密处理
        String passwd = entity.getPasswd();
        if(passwd != null && passwd.length() > 0){
            entity.setPasswd(Md5Util.md5Suffix(passwd));
        }
        String uid = entity.getUid();
        if(uid == null || uid.length() == 0){
            entity.setUid(UuidUtil.getUuid());
            return mapper.add(entity);
        }else{
            return mapper.edit(entity);
        }
    }

    /**
     * 删除会员信息表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer delete(String id) {
        return super.delete(id);
    }
}
